﻿Imports System
Imports System.Text.RegularExpressions
Imports System.Configuration
Imports dooll.Config

Public Class ModuleRewriter
    Inherits BaseModuleRewriter


    Protected Overrides Sub Rewrite(ByVal requestedPath As String, ByVal app As System.Web.HttpApplication)

        Dim urlhost As String, m As Match

        Dim sendToUrl As String = String.Empty

        Dim rules As RewriterRuleCollection = ReWriteConfig.Load()

        If app.Context.Request.Url.Port = 80 Then
            urlhost = app.Context.Request.Url.Host
        Else
            urlhost = String.Concat(app.Context.Request.Url.Host, ":", app.Context.Request.Url.Port)
        End If

        Dim RuleInfo As RewriterRule = rules.Find(requestedPath, urlhost)

        If Not RuleInfo Is Nothing Then
            Dim LookFor As String = String.Empty
            If RuleInfo.LookFor.StartsWith("http://") Then
                LookFor = String.Concat("^", RuleInfo.LookFor, "$")
            Else
                LookFor = String.Concat("^http://", urlhost, RuleInfo.LookFor, "$")
            End If
            sendToUrl = Regex.Replace(requestedPath, LookFor, RuleInfo.SendTo, RegexOptions.IgnoreCase)
            RewriterUtils.RewriteUrl(app.Context, sendToUrl)
        End If

        m = Nothing
        rules = Nothing



    End Sub
End Class

